<template>
  <a-card :bordered="false">
    <a-row :gutter="8">
      <a-col :span="5"><search-tree :treeData="deptTree" :dataList="dataList" :expandedKeys="expandedKeys" :holderText="holderText" @select="handleSelect"></search-tree></a-col>
      <a-col :span="19">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="8">
              <a-col :md="5" :sm="15">
                <a-form-item><a-input placeholder="请输入专业代码" v-model="queryParam.specialityCode" /></a-form-item>
              </a-col>
              <a-col :md="5" :sm="15">
                <a-form-item><a-input placeholder="请输入专业名称" v-model="queryParam.specialityName" /></a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div class="table-operator">
          <a-button v-if="addEnable" type="primary" icon="plus" @click="$refs.modal.add()">新建</a-button>
          <a-dropdown v-if="removeEnable && selectedRowKeys.length > 0">
            <a-popconfirm
              title="确定删除吗?"
              ok-text="是"
              cancel-text="否"
              @confirm="delByIds(selectedRowKeys)"
            >
              <a-button type="danger" icon="delete" >删除</a-button>
            </a-popconfirm>

          </a-dropdown>
        </div>
        <s-table
          size="default"
          ref="table"
          rowKey="specialityCode"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :columns="columns"
          :data="loadData"
        >
          <span slot="action" slot-scope="text, record">
            <a v-if="editEnabel" @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm
              title="确定删除吗?"
              ok-text="是"
              cancel-text="否"
              @confirm="delByIds([record.specialityCode])"
            >
            <a v-if="removeEnable" >删除</a>
          </a-popconfirm>

          </span>
        </s-table>
      </a-col>
    </a-row>
    <specialityBase-modal ref="modal" :deptTree="deptTree" @ok="handleOk" />
  </a-card>
</template>

<script>
import { STable, SearchTree } from '@/components'
import { getSpecialityBaseList, delSpecialityBase } from '@/api/speciality'
import SpecialityBaseModal from './modules/SpecialityBaseModal.vue'
import { checkPermission } from '@/utils/permissions'
import { getDeptListTeach } from '@/api/system'

export default {
  name: 'TableList',
  components: {
    STable,
    SpecialityBaseModal,
    SearchTree
  },
  data () {
    return {
      deptTree: [],
      dataList: [],
      expandedKeys: [],
      holderText: '搜索系部',
      labelCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 5
        }
      },
      wrapperCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 16
        }
      },
      form: this.$form.createForm(this),
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '专业代码',
          dataIndex: 'specialityCode'
        },
        {
          title: '专业名称',
          dataIndex: 'specialityName'
        },
        {
          title: '系别名称',
          dataIndex: 'deptName'
        },
        {
          title: '操作',
          width: '200px',
          dataIndex: 'action',
          scopedSlots: {
            customRender: 'action'
          }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getSpecialityBaseList(Object.assign(parameter, this.queryParam))
      },
      selectedRowKeys: [],
      selectedRows: [],
      addEnable: checkPermission('student:speciality:add'),
      editEnabel: checkPermission('student:speciality:edit'),
      removeEnable: checkPermission('student:speciality:remove')
    }
  },
  filters: {},
  created () {
    getDeptListTeach().then(res => {
      const data = res.rows
      this.buildtree(data, this.deptTree, 0)
      this.expandedKeys = data.map(m => m.parentId)
      this.dataList = data.map(m => {
        return {
          key: m.deptId,
          title: m.deptName
        }
      })
    })
  },
  methods: {
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleAdd () {
      this.$refs.modal.add()
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh(true)
      console.log('handleSaveOk')
    },
    delByIds (ids) {
      delSpecialityBase({
        ids: ids.join(',')
      }).then(res => {
        if (res.code === 0) {
          this.$message.success('删除成功')
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
        this.selectedRowKeys = []
      })
    },
    handleSelect (selectedKeys, { node }) {
      this.queryParam = {
        deptId: selectedKeys[0] !== 1 ? selectedKeys[0] : ''
      }
      this.$refs.table.refresh(true)
    },
    buildtree (list, arr, parentId) {
      list.forEach(item => {
        if (item.parentId === parentId) {
          var child = {
            key: item.deptId,
            value: item.deptId, // value是给modal的select用的，2者属性不一样
            title: item.deptName,
            scopedSlots: {
              title: 'title'
            },
            children: []
          }
          this.buildtree(list, child.children, item.deptId)
          if (child.children.length === 0) {
            delete child.children
          }
          arr.push(child)
        }
      })
    }
  },
  watch: {
    /*
        'selectedRows': function (selectedRows) {
          this.needTotalList = this.needTotalList.map(item => {
            return {
              ...item,
              total: selectedRows.reduce( (sum, val) => {
                return sum + val[item.dataIndex]
              }, 0)
            }
          })
        }
        */
  }
}
</script>
